package top.continew.admin.controller.distribute;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.shengchao.examstar.rpc.distribute.enums.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.continew.admin.business.service.UserHelperService;
import top.continew.admin.req.user.SysUserQuery;
import top.continew.admin.system.model.vo.UserInfoVo;
import top.continew.admin.system.model.vo.UserWithCpVo;
import top.continew.admin.system.model.vo.UserWithRoleVo;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
import top.continew.starter.web.model.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: examstar-manger-api
 * @description: 分销支撑的Rest api
 * @author: Xiong weiqiang
 * @create: 2025-03-26 17:04
 */
@Tag(name = "分销-基础支撑")
@RestController
@RequestMapping("/distribute/support")
@RequiredArgsConstructor
public class DistributeSupportController {
    private final UserHelperService userHelperService;
    @Operation(summary = "分销角色代码", description = "分销角色代码")
    @GetMapping("/roleCodes")
    R<List<LabelValueResp<String>>> distributeRoleCodes() {
        List<LabelValueResp<String>> list = new ArrayList<>();
        SysRoleDistributeBindEnum[] saleRoleEnums=SysRoleDistributeBindEnum.values();
        for (SysRoleDistributeBindEnum sysRoleDistributeBindEnum:saleRoleEnums){
            list.add(new LabelValueResp<>(sysRoleDistributeBindEnum.getDistributeName(),sysRoleDistributeBindEnum.getDistributeCode(),Boolean.FALSE));
        }
        return R.ok(list);
    }
    @Operation(summary = "分销系统角色绑定关系", description = "分销系统角色绑定关系")
    @GetMapping("/sbdCodes")
    R<List<LabelValueResp<String>>> sysBindDistributeRoleCodes() {
        List<LabelValueResp<String>> list = new ArrayList<>();
        SysRoleDistributeBindEnum[] saleRoleEnums= SysRoleDistributeBindEnum.values();
        for (SysRoleDistributeBindEnum sysRoleDistributeBindEnum:saleRoleEnums){
            list.add(new LabelValueResp<>(sysRoleDistributeBindEnum.getDistributeCode(),sysRoleDistributeBindEnum.getSysRoleCode(),Boolean.FALSE));
        }
        return R.ok(list);
    }
    @Operation(summary = "分销用户类型", description = "分销用户类型")
    @GetMapping("/distributeUserTypes")
    R<List<LabelValueResp<String>>> distributeUserTypes(Integer noEmployee) {
        List<LabelValueResp<String>> list = new ArrayList<>();
        DistributeNodePropEnum[] userPropEnums=DistributeNodePropEnum.values();
        for (DistributeNodePropEnum distributeUserPropEnum:userPropEnums){
            if (noEmployee==1){
                continue;
            }
            list.add(new LabelValueResp<>(distributeUserPropEnum.getDescription(),distributeUserPropEnum.getProp(),Boolean.FALSE));
        }
        return R.ok(list);
    }
    @Operation(summary = "按手机或名字已有用户", description = "查询已有用户")
    @GetMapping("/sysUserQuery")
    R<List<UserInfoVo>> sysUserQuery(SysUserQuery userQuery) {
        List<UserInfoVo> userInfoVos=userHelperService.queryByNumberAndUserName(userQuery.getPhone(), userQuery.getSearchValue());
           return R.ok(userInfoVos);
    }
    @Operation(summary = "查询已有分销角色的加盟商员工", description = "查询已有分销角色的加盟商员工")
    @GetMapping("/sysUserQueryForJmsYg")
    R<List<UserWithRoleVo>> sysUserQueryForJmsYg(@NotBlank String phone) {
        Integer distributeType= DistributeTypeEnum.TYPE_SALE.getType();
        List<UserWithRoleVo> roleVoList=userHelperService.queryByNumberAndRoleCode(phone, distributeType,"");
        if (CollUtil.isNotEmpty(roleVoList)){
            if (roleVoList.size()==1&&roleVoList.get(0).getUserId()==null){
                roleVoList.remove(0);
            }
            for (UserWithRoleVo userWithRoleVo:roleVoList){
                if (DistributeSaleRoleEnum.ROLE_DSPD_EMP.getCode().equals(userWithRoleVo.getRoleCodeListStr())){
                    userWithRoleVo.setDisabled(false);
                }else {
                    userWithRoleVo.setDisabled(true);
                }
            }
        }
        return R.ok(roleVoList);
    }
    @Operation(summary = "查询已有分销角色的加盟商员工(带企微号", description = "查询已有分销角色的加盟商员工(带企微号")
    @GetMapping("/sysUserWithCpQueryForJmsYg")
    R<List<UserWithRoleVo>> sysUserWithCpQueryForJmsYg(@NotBlank String phone) {
        List<UserWithCpVo> roleVoList=userHelperService.selectUserWithCpByPhone(phone);
        if (CollUtil.isNotEmpty(roleVoList)){
            if (roleVoList.size()==1&&roleVoList.get(0).getUserId()==null){
                roleVoList.remove(0);
            }
            for (UserWithCpVo UserWithCpVo:roleVoList){
                UserWithCpVo.setDisabled(false);
            }
        }
        return R.ok(roleVoList);
    }
    @Operation(summary = "查询已有用户用于创建", description = "查询已有用户用于创建")
    @GetMapping("/sysUserQueryForDistributeCreate")
    R<List<UserWithRoleVo>> distributeUserForDistributeCreate(@NotBlank String phone, @NotBlank String roleCode) {
        SysRoleDistributeBindEnum sysRoleDistributeBindEnum = SysRoleDistributeBindEnum.getByDistributeCode(roleCode);
        if (sysRoleDistributeBindEnum==null){
            return R.fail(R.fail().getCode(), "系统错误");
        }
        Integer distributeType= DistributeTypeEnum.TYPE_SALE.getType();
        if (Arrays.asList(5L,6L).contains(sysRoleDistributeBindEnum.getSysRoleId())){
            distributeType= DistributeTypeEnum.TYPE_CTPD.getType();
        }
        List<UserWithRoleVo> roleVoList=userHelperService.queryByNumberAndRoleCode(phone, distributeType,sysRoleDistributeBindEnum.getSysRoleCode());
        if (CollUtil.isNotEmpty(roleVoList)){
            if (roleVoList.size()==1&&roleVoList.get(0).getUserId()==null){
                roleVoList.remove(0);
            }

            for (UserWithRoleVo userWithRoleVo:roleVoList){
                userWithRoleVo.setDisabled(isDistributeUserCreateDisabled(userWithRoleVo,roleCode));
            }
        }
        return R.ok(roleVoList);
    }

    @Operation(summary = "查询已有用户用于上级切换", description = "查询已有用户用于上级切换")
    @GetMapping("/sysUserQueryForDistributeUpLevelChange")
    R<List<UserWithRoleVo>> distributeUserForDistributeUpLevelChange(@NotBlank String phone, @NotBlank String roleCode) {
        SysRoleDistributeBindEnum sysRoleDistributeBindEnum = SysRoleDistributeBindEnum.getByDistributeCode(roleCode);
        if (sysRoleDistributeBindEnum==null){
            return R.fail(R.fail().getCode(), "系统错误,错误角色值"+roleCode);
        }
        Integer distributeType= DistributeTypeEnum.TYPE_SALE.getType();
        if (Arrays.asList(5L,6L).contains(sysRoleDistributeBindEnum.getSysRoleId())){
            distributeType= DistributeTypeEnum.TYPE_CTPD.getType();
        }
        List<UserWithRoleVo> roleVoList=userHelperService.queryByNumberAndRoleCode(phone, distributeType,sysRoleDistributeBindEnum.getSysRoleCode());
        if (CollUtil.isNotEmpty(roleVoList)){
            if (roleVoList.size()==1&&roleVoList.get(0).getUserId()==null){
                roleVoList.remove(0);
            }
            for (UserWithRoleVo userWithRoleVo:roleVoList){
                userWithRoleVo.setDisabled(isDistributeUserUpLevelDisabled(userWithRoleVo,roleCode));
            }
        }
        return R.ok(roleVoList);
    }

    /**
     * 判断是否可以创建
     * @param userWithRoleVo
     * @param roleCode
     * @return
     */
    private boolean isDistributeUserCreateDisabled(UserWithRoleVo userWithRoleVo,String roleCode){
        //新用户同体系没角色才可以
        if (DistributeSaleRoleEnum.getUpperRoleByCode(roleCode)!=null){
            if (userWithRoleVo.getRoleCodeListStr()!=null&& userWithRoleVo.getRoleCodeListStr().contains(roleCode)){
                if (StrUtil.isNotBlank(userWithRoleVo.getRoleCodeListStr())){
                    if (userWithRoleVo.getRoleCodeListStr().contains(DistributeSaleRoleEnum.ROLE_DSPD.getCode())
                            ||userWithRoleVo.getRoleCodeListStr().contains(DistributeSaleRoleEnum.ROLE_DSPD_EMP.getCode())
                            ||userWithRoleVo.getRoleCodeListStr().contains(DistributeSaleRoleEnum.ROLE_MKMG.getCode())
                            ||userWithRoleVo.getRoleCodeListStr().contains(DistributeSaleRoleEnum.ROLE_SLMB.getCode())
                    ){
                        return Boolean.TRUE;
                    }
                }
            }
        }
        if (DistributeContentRoleEnum.getByCode(roleCode)!=null){
            if (userWithRoleVo.getRoleCodeListStr()!=null&& userWithRoleVo.getRoleCodeListStr().contains(roleCode)){
                if (StrUtil.isNotBlank(userWithRoleVo.getRoleCodeListStr())){
                    if (userWithRoleVo.getRoleCodeListStr().contains(DistributeContentRoleEnum.ROLE_CTPD.getCode())
                            ||userWithRoleVo.getRoleCodeListStr().contains(DistributeContentRoleEnum.ROLE_CTPD_EMP.getCode())
                    ){
                        return Boolean.TRUE;
                    }
                }
            }
        }
        return Boolean.FALSE;
    }

    /**
     * 判断是否可以当上级
     * @param userWithRoleVo
     * @param roleCode
     * @return
     */
    private boolean isDistributeUserUpLevelDisabled(UserWithRoleVo userWithRoleVo,String roleCode){
        DistributeSaleRoleEnum distributeSaleRoleEnum = DistributeSaleRoleEnum.getByCode(roleCode);
        //换上级指定角色才可以
        if (distributeSaleRoleEnum!=null){
            if (userWithRoleVo.getRoleCodeListStr()!=null) {
                switch (distributeSaleRoleEnum){
                    case ROLE_DSPD:
                        return !(userWithRoleVo.getRoleCodeListStr().contains(DistributeSaleRoleEnum.ROLE_MKMG.getCode()));
                    case ROLE_DSPD_EMP:
                        return !(userWithRoleVo.getRoleCodeListStr().contains(DistributeSaleRoleEnum.ROLE_DSPD.getCode()));
                    case ROLE_SLMB:
                        return !(userWithRoleVo.getRoleCodeListStr().contains(DistributeSaleRoleEnum.ROLE_DSPD.getCode()));

                }
            }
            return Boolean.TRUE;
        }
        DistributeContentRoleEnum contentRoleEnum=DistributeContentRoleEnum.getByCode(roleCode);
        if (contentRoleEnum!=null){
                return !(StrUtil.isNotBlank(userWithRoleVo.getRoleCodeListStr())&&userWithRoleVo.getRoleCodeListStr().equals(DistributeContentRoleEnum.ROLE_CTPD.getCode()));
        }
        return Boolean.TRUE;
    }
}